package helloWorld;

import org.mt4j.MTApplication;
import org.mt4j.components.MTCanvas;
import org.mt4j.components.interfaces.IMTComponent3D;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomEvent;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;


public class AlbumGestureListener implements IGestureEventListener {
	private MTApplication mtApplication;
	private Controller controller;
	public AlbumGestureListener(MTApplication mtApplication){
		this.mtApplication = mtApplication;
		controller = Controller.getInstance();
	}

	@Override
	public boolean processGestureEvent(MTGestureEvent ge) {
		if(ge instanceof TapEvent){
			TapEvent te = (TapEvent)ge;
			IMTComponent3D target = te.getTargetComponent();
			if(target instanceof MTImage){
				// go back
				if(target.getName() == "albumsceneBACK"){
					mtApplication.changeScene(mtApplication.getScene("ArtistScene"));
				}
			}
			else if (target instanceof MTRectangle) {
				MTRectangle rectangle = (MTRectangle) target;
				switch (te.getTapID()) {
				case TapEvent.BUTTON_DOWN:
					System.out.println("Button down state on " + target);
					rectangle.setFillColor(new MTColor(200,100,100));
					break;
				case TapEvent.BUTTON_UP:
					System.out.println("Button up state on " + target);
					rectangle.setFillColor(new MTColor(255,255,255));
					break;
				case TapEvent.BUTTON_CLICKED:
					System.out.println("Button clicked state on " + target);
					rectangle.setFillColor(new MTColor(255,255,255));
					break;
				default:
					break;
				}
			}else if(target instanceof MTCanvas){
				MTCanvas rectangle = (MTCanvas) target;
				switch (te.getTapID()) {
				case TapEvent.BUTTON_DOWN:
					System.out.println("Button down state on " + target);
					float curX = te.getCursor().getCurrentEvtPosX();
					float curY = te.getCursor().getCurrentEvtPosY();
					Vector3D origin = target.getViewingCamera().getPosition();
					System.out.println("Current cam position: \n" + origin.getX() +
							"\n" + origin.getY() + "\n" + origin.getZ());
					
					//target.getViewingCamera().setPosition(new Vector3D(curX,curY));
					break;
				case TapEvent.BUTTON_UP:
					System.out.println("Button up state on " + target);
					
					break;
				case TapEvent.BUTTON_CLICKED:
					System.out.println("Button clicked state on " + target);
					
					break;
				default:
					break;
				}
			}
			
		} else if (ge instanceof ZoomEvent){
			ZoomEvent ze = (ZoomEvent)ge;
			if(ze.getTargetComponent().getName() == "albumcanvas")
			{
				if(ze.getCamZoomAmount() >= 10)
					;//mtApplication.changeScene(mtApplication.getScene("ArtistScene"));
			}
			else
			{
				SceneAlbum target = (SceneAlbum)ze.getTargetComponent();
				System.out.println("Change to albumInfo" + target.getAlbum());
				controller.setCurrentAlbum(target.getAlbum());
				Controller.getInstance().setCurrentAlbum(target.getAlbum());
				mtApplication.changeScene(mtApplication.getScene("AlbumInfoScene"));
			}
		}else if (ge instanceof DragEvent){
			DragEvent de = (DragEvent)ge;
			
			SceneAlbum target = (SceneAlbum)de.getTargetComponent();
			
			if(de.getTo().x > 9*(mtApplication.width/10) && de.getTo().y > 9*(mtApplication.height/10f))
			{
				DBInterface.getInstance().removeAlbumFromDB(target.getAlbum());
				target.unregisterAllInputProcessors();
				target.removeFromParent();
				target.destroy();
			}
			else
				target.setPositionGlobal(de.getTo());
		}
		return false;

	}
	
	
}
